﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace MyTetris
{
    public class Square
    {
        //小方块的边长
        public const int SQUARE_WIDTH = 25;
        //小方块的X坐标（像素）
        public int PixelLeft { set; get; }
        //小方块的Y坐标（像素）
        public int PixelTop { set; get; }

        //小方块的索引X坐标
        public int IndexLeft
        {
            get
            {
                return PixelLeft / Square.SQUARE_WIDTH;
            }
        }
        //小方块的索引Y坐标
        public int IndexTop
        {
            get
            {
                return PixelTop / SQUARE_WIDTH;
            }
        }

        public Color FillColor { set; get; }

        //判断小方块能否向下移动
        public bool CanDown()
        {
            //当小方块的索引Y左边超过游戏的边界，则方块不能继续下落
           if (IndexTop + 1 >= GameEngine.FIELD_H)
                return false;
           //若小方块下落一格的地方已经有一个小方块，则不能继续下落
            if (GameEngine.Instance.matrix[IndexTop + 1, IndexLeft] == 1)
                return false;
            return true;
        }
        //向下移动一格
        public void MoveDown()
        {
            if(CanDown())
            {
                //小方块的像素坐标加上一个基础小块的边长
                PixelTop += SQUARE_WIDTH;
            }
            else
            {
                //停止不动
                GameEngine.Instance.matrix[IndexTop, IndexLeft] = 1;
            }
        }
        //判断小方块能否向左移动
        public bool CanLeft()
        {
            //如果游戏已经结束，则不能继续向左移动
            if (GameEngine.Instance.IsSelfGameOver)
                return false;
            //如果像素坐标已经小于等于零（到达游戏边界),则不能继续向左移动
            if (PixelLeft <= 0)
                return false;
            //如果小方块的左边已经存在一个小方块，则不能继续向左移动
            if (GameEngine.Instance.matrix[IndexTop, IndexLeft - 1] == 1)
                return false;
            return true;
        }
        //向左移动一格
        public void MoveLeft()
        {
            if (CanLeft())
            {
                //像素坐标减去一个基础小块的边长
                PixelLeft -= SQUARE_WIDTH;
                //方块的中心X坐标也减一
                CenterIndexX--;
            }
        }
        //判断小方块能否向右移动
        public bool CanRight()
        {
            //游戏结束，不能右移
            if (GameEngine.Instance.IsSelfGameOver)
                return false;
            //像素坐标加上一个基础方块的边长大于游戏边界时，不能右移
            if (PixelLeft + GameEngine.SQUARE_WIDTH >= GameEngine.Instance.FieldIndexWidth)
                return false;
            if (GameEngine.Instance.matrix[IndexTop, IndexLeft + 1] == 1)
                return false;
            return true;
        }
        //向右移动一格
        public void MoveRight()
        {
            if (CanRight())
            {
                //像素坐标加上一个小方块的坐标
                PixelLeft += SQUARE_WIDTH;
                //方块的中心X坐标也加一
                CenterIndexX++;
            }
        }
        //绘制小方块
        public void Draw(Graphics g)
        {
            g.FillRectangle(Brushes.Bisque, PixelLeft, PixelTop,
                SQUARE_WIDTH, SQUARE_WIDTH);
            g.DrawRectangle(Pens.Yellow, PixelLeft, PixelTop,
                SQUARE_WIDTH, SQUARE_WIDTH);
        }

        //小方块的旋转X坐标
        public int CenterIndexX { set; get; }
        //小方块的旋转Y坐标
        public int CenterIndexY { set; get; }

        //判断小方块能否旋转
        public bool CanRotate()
        {
            //如果游戏结束，则不能旋转
            if (GameEngine.Instance.IsSelfGameOver)
                return false;
            int targetIndexLeft = CenterIndexX + CenterIndexY - IndexTop;
            int targetIndexTop = CenterIndexY + IndexLeft - CenterIndexX;
            if (targetIndexLeft < 0 || targetIndexLeft >= GameEngine.FIELD_W || targetIndexTop < 0 || targetIndexTop >= GameEngine.FIELD_H)
                return false;
            return GameEngine.Instance.matrix[targetIndexTop, targetIndexLeft] == 0;

        }
        //小方块的旋转
        public void Rotate()
        {
            if(CanRotate())
            {
                if (!CanRotate())
                    return;
                int tempLeft = PixelLeft;
                int tempTop = PixelTop;
                PixelLeft = CenterIndexX * GameEngine.SQUARE_WIDTH + CenterIndexY * GameEngine.SQUARE_WIDTH - tempTop;
                PixelTop = CenterIndexY * GameEngine.SQUARE_WIDTH + tempLeft - CenterIndexX * GameEngine.SQUARE_WIDTH;
            }
        }

    }
}
